﻿using HY.Entity;
using HY.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace HY.Web.Controllers
{
    public class BaseController : Controller
    {
        /// <summary>
        /// 当前用户信息
        /// </summary>
        public V_UserInfo UserInfo = GlobalSupport.CurrentUser;

        public string NoDataMsg = "未获取到相应数据";

        /// <summary>
        /// 获取列表需要的返回值
        /// </summary>
        /// <param name="tuple"></param>
        /// <returns></returns>
        public ReturnModel GetReturnModel(int count, object data)
        {
            return new ReturnModel() { code = 0, count = count, msg = count > 0 ? "" : NoDataMsg, data = data };
        }

        /// <summary>
        /// 出现异常时的返回值
        /// </summary>
        /// <param name="ex"></param>
        /// <returns></returns>
        public ReturnModel GetReturnModel(Exception ex)
        {
            return new ReturnModel() { code = -1, count = 0, msg = ex.Message, data = null };
        }

        #region 解决JsonResult日期格式问题
        /// <summary>
        /// 重写Json方法（通过Controller类Json方法源码可以看出，所有Json方法最终都会调用下列方法，因此重写该方法，修改该方法返回值即可）
        /// </summary>
        /// <param name="data"></param>
        /// <param name="contentType"></param>
        /// <param name="contentEncoding"></param>
        /// <param name="jsonRequest"></param>
        /// <returns></returns>
        protected override JsonResult Json(object data, string contentType, Encoding contentEncoding, JsonRequestBehavior jsonRequest)
        {
            return new JsonResultExtension { Data = data, ContentType = contentType, ContentEncoding = contentEncoding, JsonRequestBehavior = jsonRequest };
        }
        #endregion
    }
}